﻿using Jugg.Admin.Framework.Infrastructure;
using Jugg.Admin.Framework.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Jugg.Admin.Framework.Mvc.Filters
{
    /// <summary>
    /// 是否已经登陆，已经登陆直接跳转
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class AdminLoginedAttribute : ActionFilterAttribute
    {
        private string _routeName;

        public AdminLoginedAttribute(string routeName)
        {
            this._routeName = routeName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var authentication = Core.EngineContext.Current.Resolve<IAuthenticationService>();
            if (authentication.isAuthenticated())
            {
                var user = authentication.CurrentUser;
                if (user != null && user.Enabled)
                    filterContext.Result = new RedirectToRouteResult(_routeName, new System.Web.Routing.RouteValueDictionary());
            }
        }
    }
}
